﻿function pampaAjaxPost(url, data, callback, context) {
    var response;

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        success: function (result) {
            if (result.error)
                response = { context: context, error: result.error };
            else
                response = { context: context, value: result.value };

            if (callback)
                callback(response);
        },
        async: (callback != undefined)
    });

    if (!callback)
        return response;
}

var Ajax =
{
    AppPath: "/"
};

var CustomGrid =
{
    Refresh: function (gridId, callback, context) {
        var data = { gridId: gridId, pageNumber: pageNumber };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/Refresh", data, callback, context);
    },

    RefreshSamePage: function (gridId, callback, context) {
        var data = { gridId: gridId };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/RefreshSamePage", data, callback, context);
    },

    MoveTo: function (gridId, pageNumber, callback, context) {
        var data = { gridId: gridId, position: pageNumber };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/MoveTo", data, callback, context);
    },

    UpdateConfiguration: function (gridId, configuration, callback, context) {
        var data = { gridId: gridId, configuration: encodeURIComponent(configuration) };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/UpdateConfiguration", data, callback, context);
    },

    UpdateQuickFilter: function (gridId, configuration, callback, context) {
        var data = { gridId: gridId, configuration: encodeURIComponent(configuration) };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/UpdateQuickFilter", data, callback, context);
    },

    GetCurrentConfiguration: function (gridId, callback, context) {
        var data = { gridId: gridId, configuration: encodeURIComponent(configuration) };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/GetCurrentConfiguration", data, callback, context);
    },

    CleanFilters: function (gridId, callback, context) {
        var data = { gridId: gridId };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/CleanFilters", data, callback, context);
    },

    OrderBy: function (gridId, field, callback, context) {
        var data = { gridId: gridId, field: field };
        pampaAjaxPost(Ajax.AppPath + "CustomGrid/OrderBy", data, callback, context);
    }
};

var EntityComboList =
{
    GetList: function (controlId, entityName, property, value, orderBy, propertyId, propertyDesc, callback, context) {
        var data = { controlId: controlId, entityName: entityName, property: property, value: value, orderBy: orderBy, propertyId: propertyId, propertyDesc: propertyDesc };
        return pampaAjaxPost(Ajax.AppPath + "EntityComboList/GetList", data, callback, context);
    }
};

var EntitySelector =
{
    GetList: function (controlId, entityName, value, maxtam, propertyId, propertyDesc, callback, context) {
        var data = { controlId: controlId, entityName: entityName, value: value, maxtam: maxtam, propertyId: propertyId, propertyDesc: propertyDesc };
        pampaAjaxPost(Ajax.AppPath + "EntitySelector/GetList", data, callback, context);
    },

    GetFilteredList: function (controlId, entityName, value, maxtam, property, parentValue, propertyId, propertyDesc, callback, context) {
        var data = { controlId: controlId, entityName: entityName, value: value, maxtam: maxtam, property: property, parentValue: parentValue, propertyId: propertyId, propertyDesc: propertyDesc };
        pampaAjaxPost(Ajax.AppPath + "EntitySelector/GetFilteredList", data, callback, context);
    }
};

var EntityCheckList =
{
    GetList: function (controlId, entityName, property, parentValue, orderBy) {
        var data = { controlId: controlId, entityName: entityName, property: property, parentValue: parentValue, orderBy: orderBy };
        return pampaAjaxPost(Ajax.AppPath + "EntityCheckList/GetList", data);
    },

    GetList: function (controlId, entityName, property, parentValue, orderBy, callback, context) {
        var data = { controlId: controlId, entityName: entityName, property: property, parentValue: parentValue, orderBy: orderBy };
        pampaAjaxPost(Ajax.AppPath + "EntityCheckList/GetList", data, callback, context);
    }
};

var EntityEdit =
{
    UpdateValues: function (sessionID, xml) {
        var data = { sessionID: sessionID, xml: encodeURIComponent(xml) };
        return pampaAjaxPost(Ajax.AppPath + "EntityEdit/UpdateValues", data);
    },

    Save: function (sessionID, xml) {
        var data = { sessionID: sessionID, xml: encodeURIComponent(xml) };
        return pampaAjaxPost(Ajax.AppPath + "EntityEdit/Save", data);
    }
};

var EntityView =
{
    GetHTML: function (entityName, entityID, strOrientation) {
        var data = { entityName: entityName, entityID: entityID, strOrientation: strOrientation };
        return pampaAjaxPost(Ajax.AppPath + "EntityView/GetHTML", data);
    }
};

var FilterManager =
{
    GetDescription: function (entityTypeQualifiedName, strValue) {
        var data = { entityTypeQualifiedName: entityTypeQualifiedName, strValue: strValue };
        return pampaAjaxPost(Ajax.AppPath + "FilterManager/GetDescription", data);
    }
};

